package com.wander.designCreational.simplefactory.client;

import com.wander.designCreational.simplefactory.factory.Factory;
import com.wander.designCreational.simplefactory.factory.FactoryMoreMethod;
import com.wander.designCreational.simplefactory.factory.FactoryPlus;
import com.wander.designCreational.simplefactory.product.ApplePhoneImpl;
import com.wander.designCreational.simplefactory.product.Phone;
import com.wander.designCreational.simplefactory.product.RedmiPhoneImpl;
import org.junit.jupiter.api.Test;

/**
 * @Author : WDYin
 * @Date : 2020/5/8
 * @Description :
 */
public class Client {

    @Test
    public void test1() {
        /*优点：只需要传入一个正确的参数，就可以获取你所需要的对象而无需知道其创建细节
         * 缺点：工厂类的职责相对锅中，增加新的产品需要修改工厂类的判断逻辑，违背开闭原则*/
        Phone redmiPhone = Factory.getPhone("红米");
        System.out.println(redmiPhone);
        redmiPhone.produce();

        Phone applePhone = Factory.getPhone("苹果");
        System.out.println(applePhone);
        applePhone.produce();
    }

    @Test
    public void test2() throws Exception {

        /*通过上面的代码，证实了之前提到的缺点（增加新的产品需要修改工厂类的判断逻辑），
        这里我们可以通过反射来修改工厂类进行改进：这样当我们在新增产品的时候就不在需要修改工厂类了。*/
        Phone redmiPhone = FactoryPlus.getPhone(RedmiPhoneImpl.class);
        System.out.println(redmiPhone);
        redmiPhone.produce();

        Phone applePhone = FactoryPlus.getPhone(ApplePhoneImpl.class);
        System.out.println(applePhone);
        applePhone.produce();
    }

    @Test
    public void test3() throws Exception {
        Phone apple = FactoryMoreMethod.getApple();
        apple.produce();

        Phone redmi = FactoryMoreMethod.getRedmi();
        redmi.produce();

        Phone honor = FactoryMoreMethod.getHonor();
        honor.produce();
    }
}
